package com.lzb.compressmaster.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseNoModelFragment;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.activity.GoCompressActivity;
import com.lzb.compressmaster.adapter.DocumentsAdapter;
import com.lzb.compressmaster.adapter.SelAudioAdapter;
import com.lzb.compressmaster.basic.media.AudioBean;
import com.lzb.compressmaster.basic.media.MediaInfo;
import com.lzb.compressmaster.basic.media.MediaLoader;
import com.lzb.compressmaster.databinding.FragmentAudioBinding;
import com.lzb.compressmaster.databinding.FragmentDocumentBinding;
import com.lzb.compressmaster.utils.CompressFileManager;
import com.lzb.compressmaster.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DocumentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DocumentFragment extends BaseNoModelFragment<FragmentDocumentBinding> implements OnItemClickListener {

    private CompressFileManager mInstance;
    private DocumentsAdapter documentsAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_document;
    }

    @Override
    protected void initView() {
        mInstance = CompressFileManager.getInstance();

        mDataBinding.rvDocumentList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        documentsAdapter = new DocumentsAdapter();
        documentsAdapter.setEdit(true);
        mDataBinding.rvDocumentList.setAdapter(documentsAdapter);
        documentsAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        documentsAdapter.getItem(position).setSelected(!documentsAdapter.getItem(position).isSelected());
        documentsAdapter.notifyDataSetChanged();
        boolean hasExist = false;
        int tmpPosition = -1;
        for (String selectPath : mInstance.getSelectFileBeans()) {
            tmpPosition++;
            if (documentsAdapter.getItem(position).getPath().equals(selectPath)) {
                hasExist = true;
                break;
            }
        }
        if (hasExist) {
            mInstance.deleteSelectFile(tmpPosition);
        } else {
            mInstance.addSelectFile(documentsAdapter.getItem(position).getPath());
        }
        ((GoCompressActivity) mContext).refreshDataCount(mInstance);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        //设置获取的数据类型
        List<String> type = new ArrayList<>();
        type.add("application/msword");
        type.add("application/vnd.ms-excel");
        type.add("application/vnd.ms-powerpoint");
        type.add("application/pdf");
        type.add("text/plain");
        RxUtil.create(new RxUtil.Callback<List<MediaInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MediaInfo>> emitter) {
                List<MediaInfo> infoList = MediaLoader.loadDoc(type);
                emitter.onNext(infoList);
            }

            @Override
            public void accept(List<MediaInfo> infoList) {
                if (infoList != null && infoList.size() != 0) {
                    documentsAdapter.setList(infoList);
                    mDataBinding.rvDocumentList.setVisibility(View.VISIBLE);
                    mDataBinding.flDocumentNotData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvDocumentList.setVisibility(View.GONE);
                    mDataBinding.flDocumentNotData.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}